package com.swinginwind.flexutils.functions
{
	import mx.collections.IList;
	import flash.utils.ByteArray;

	/**
	 * @author jimmy
	 * @E-mail: swinginwind@gmail.com
	 * @version 1.0.0
	 * 创建时间：2013-6-14 下午6:14:47
	 * 
	 */
	public class UtilsString
	{
		public function UtilsString()
		{
		}
		
		public static function concatStringByArray(data:*, spiler:String = ",", field:String=null):String
		{
			var result:String = "";
			if(!data)
				return null;
			if(data is String)
			{
				return data as String;
			}
			if(data is Array || data is IList)
			{
				for(var i:uint=0;i<data.length;i++)
				{
					if(i != 0)
						result += spiler + (field ? data[i][field] : data[i]);
					else
						result += field ? data[i][field] : data[i];
				}
			}
			return result;
		}
		
		public static function str2unicode(str:String):String //字符串转换为unicode编码
		{
			return uniBigEnd2uni(str2uniBigEnd(str));
		}
		public static function str2uniBigEnd(ori:String):String//字符串转换为uniBigEnd编码
		{
			var str:String = "";
			for (var i:int=0; i<ori.length; i++)
			{
				if (ori.charCodeAt(i).toString(16).length < 4)
				{
					str = str + "00" + ori.charCodeAt(i).toString(16);
				}
				else
				{
					str = str + (ori.charCodeAt(i).toString(16));
				}
			}
			return str;
		}
		public static function uniBigEnd2uni(ori:String):String//uniBigEnd编码转换为unicode编码
		{
			var str1:String;
			var str:String;
			var oriarray:Array = new Array();
			oriarray = ori.match(/../g);
			for (var i:int=0; i<oriarray.length; i++)
			{
				if (i % 2 == 0)
				{
					str = oriarray[i];
					oriarray[i] = oriarray[i + 1];
					oriarray[i + 1] = str;
				}
			}
			str1 = (oriarray.toString()).replace(/,/g,"");
			return str1;
		}
		
		
		public static function str2utf8(str:String):String//字符串转换为utf8编码
		{
			var strnew:String = "";
			var byte:ByteArray = new ByteArray();
			byte.writeMultiByte(str,"UTF-8");
			for (var i:int=0; i<byte.length; i++)
			{
				strnew = strnew + byte[i].toString(16);
			}
			return strnew;
		}
	}
}